package com.xjnt.base.support.admin.shiro.credential;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import com.jfinal.kit.HashKit;

/**
 * 自定义密码验证类
 * @FileName : ShiroCredentialMatcher.java
 * @Description : 
 * @Copyright : Copyright (c) 2015
 * @author caoyong
 * @date Jul 31, 2015 2:48:01 PM
 */
public class ShiroCredentialMatcher extends SimpleCredentialsMatcher{

	/* (non-Javadoc)
	 * @see org.apache.shiro.authc.credential.SimpleCredentialsMatcher#doCredentialsMatch(org.apache.shiro.authc.AuthenticationToken, org.apache.shiro.authc.AuthenticationInfo)
	 */
	@Override
	public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        Object tokenCredentials = encrypt(String.valueOf(token.getPassword()));
        Object accountCredentials = getCredentials(info);
        //将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
        return equals(tokenCredentials, accountCredentials);
	}
	
	//将传进来密码加密方法
    private String encrypt(String password) {
        String encryptPass = HashKit.md5(password);
        return encryptPass;
    }
}
